package bbs.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xkh.xkh.R;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import bbs.model.Submit;
import utils.TimeUtil;

/**
 * 自己所发的帖子
 * Created by nimi on 2015/11/19.
 */
public class SubmitsAdapter extends BaseAdapter {
    private List<Submit> submits;
    private Context mContext;
    long days;
    long hours;

    public void clean()
    {
        submits.clear();
    }

    public SubmitsAdapter(Context context, List<Submit> submits) {
        this.mContext = context;
        this.submits = submits;
    }

    //返回已获得数据源总数量
    public int getCount() {
        // TODO Auto-generated method stub
        if (submits != null)
            return submits.size();
        else
            return 0;
    }

    @Override
    //告诉适配器取得目前容器中的数据对象
    public Submit getItem(int position) {
        // TODO Auto-generated method stub
        return submits == null ? null : submits.get(position);

    }

    @Override
    //告诉适配器取得目前容器中的数据ID
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    //取得当前欲显示的图像View
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            LayoutInflater _LayoutInflater = LayoutInflater.from(mContext);
            convertView = _LayoutInflater.inflate(R.layout.bbs_subject_item2, null);
            viewHolder.text1 = (TextView) convertView.findViewById(R.id.bbsDisplayTitle);
            viewHolder.text2 = (TextView) convertView.findViewById(R.id.postTime);
            viewHolder.text3 = (TextView) convertView.findViewById(R.id.postName);
            viewHolder.text4 = (TextView) convertView.findViewById(R.id.count);
            viewHolder.text = (TextView) convertView.findViewById(R.id.replyCnt);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.text1.setText(submits.get(position).getBoard_Title());

        String time = submits.get(position).getBoard_IssueTime();
        String timeNow = TimeUtil.currentTime();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d1 = df.parse(time);
            Date d2 = df.parse(timeNow);
            long diff = d2.getTime() - d1.getTime();
            days = diff / (1000 * 60 * 60 * 24);
            hours = diff / (1000 * 60 * 60);

        } catch (Exception e) {

        }
        if (days >= 1)
            viewHolder.text2.setText((int) days + "天前");
        else {
            viewHolder.text2.setText((int) hours + "小时前");
        }
        viewHolder.text3.setText(submits.get(position).getUser_RealName() + ":");
//        viewHolder.text4.setText(submits.get(position).getCommitbackcount() + "");
//        viewHolder.text.setText("回复：");
        return convertView;
    }

    static class ViewHolder {
        TextView text1;
        TextView text2;
        TextView text3;
        TextView text4;
        TextView text;
    }
}
